﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace POS
{
    public partial class ClockINOUTScreen : DevExpress.XtraEditors.XtraUserControl
    {
        int ServerID;
        

        public ClockINOUTScreen()
        {
            InitializeComponent();
            listView1.BackColor = this.BackColor;
            listView1.ForeColor = this.ForeColor;

            LoadEmployeeInfo();
        }

        public void LoadEmployeeInfo()
        {
            ServerID = StaticProperties.ServerID;
            DateTime ClockIN;
            DateTime ClockOut;
            DateTime Date;
            TimeSpan DayTotal;

            foreach (DataRow Employee in DataTableClass.EmployeeLogin.Rows)
            {
                if (Employee["EmployeeID"].ToString() == ServerID.ToString())
                {
                    lblEmployeeName.Text = Employee["EmployeeFullName"].ToString().Trim();
                    lblCurrentDateTime.Text = DateTime.Now.ToShortDateString() + "  " + DateTime.Now.ToLongTimeString();

                    foreach (DataRow item in DataTableClass.EmployeeTimeSheet.Rows)
                    {
                        if (item["EmployeeID"].ToString() == ServerID.ToString())
                        {
                            
                            try
                            {
                   
                                if (DateTime.TryParse((item["Date"].ToString().Trim()), out Date)) 
                                {

                                }

                                else
                                {

                                }

                                if (DateTime.TryParse((item["ClockIN"].ToString().Trim()), out ClockIN)) 
                                {

                                }

                                else
                                {

                                }

                                if (DateTime.TryParse((item["ClockOUT"].ToString().Trim()), out ClockOut)) 
                                {

                                }

                                else
                                {

                                }

                                DayTotal = ClockOut - ClockIN;

                                if (ClockOut == default(DateTime))
                                {
                                    if (DateTime.Parse((item["Date"].ToString().Trim())) != DateTime.Today)
                                    {
                                        MessageBox.Show("YOU HAVE A MISSING TIME PUNCH. PLEASE SEE MANAGEMENT ASAP!!!");
                                    }

                                    ListViewItem item1 = new ListViewItem(Date.ToShortDateString());
                                    item1.SubItems.Add(ClockIN.ToLongTimeString());
                                    item1.SubItems.Add("");
                                    item1.SubItems.Add("");
                                    item1.SubItems.Add(item["EntryID"].ToString().Trim());

                                    listView1.Items.Add(item1);
                                }
                                else
                                {

                                    ListViewItem item1 = new ListViewItem(Date.ToShortDateString());
                                    item1.SubItems.Add(ClockIN.ToLongTimeString());
                                    item1.SubItems.Add(ClockOut.ToLongTimeString());
                                    item1.SubItems.Add(DayTotal.TotalHours.ToString());
                                    item1.SubItems.Add(item["EntryID"].ToString().Trim());

                                    listView1.Items.Add(item1);
                                }
                            }
                            catch (Exception E)
                            {
                                MessageBox.Show(E.Message);
                            }


                        }
                    }

                    //foreach (DataRow item in DataTableClass.EmployeeSchedule.Rows)
                    //{
                    //if (item["EmployeeID"].ToString() == ServerID.ToString())
                    //{
                    DateTime Today = DateTime.Today;
                    int offset = Today.DayOfWeek - DayOfWeek.Sunday;

                    DateTime lastSunday = Today.AddDays(-offset);

                    FillSchedule(lastSunday);
                    //}
                    //}
                }
            }
        }

        private void FillSchedule(DateTime WeekDate)
        {
            lblWeekOfDate.Text = WeekDate.ToLongDateString();

            foreach (DataRow item in DataTableClass.EmployeeSchedule.Rows)
            {
                if (item["EmployeeID"].ToString() == ServerID.ToString())
                {
                    lblMondayAM.Text = item["MondayAM"].ToString().Trim();
                    lblMondayPM.Text = item["MondayPM"].ToString().Trim();
                    lblTuesdayAM.Text = item["TuesdayAM"].ToString().Trim();
                    lblTuesdayPM.Text = item["TuesdayPM"].ToString().Trim();
                    lblWednesdayAM.Text = item["WednesdayAM"].ToString().Trim();
                    lblWednesdayPM.Text = item["WednesdayPM"].ToString().Trim();
                    lblThursdayAM.Text = item["ThursdayAM"].ToString().Trim();
                    lblThursdayPM.Text = item["ThursdayPM"].ToString().Trim();
                    lblFridayAM.Text = item["FridayAM"].ToString().Trim();
                    lblFridayPM.Text = item["FridayPM"].ToString().Trim();
                    lblSaturdayAM.Text = item["SaturdayAM"].ToString().Trim();
                    lblSaturdayPM.Text = item["SaturdayPM"].ToString().Trim();
                    lblSundayAM.Text = item["SundayAM"].ToString().Trim();
                    lblSundayPM.Text = item["SundayPM"].ToString().Trim();
                }
                else
                {
                    lblMondayAM.Text = "";
                    lblMondayPM.Text = "";
                    lblTuesdayAM.Text = "";
                    lblTuesdayPM.Text = "";
                    lblWednesdayAM.Text = "";
                    lblWednesdayPM.Text = "";
                    lblThursdayAM.Text = "";
                    lblThursdayPM.Text = "";
                    lblFridayAM.Text = "";
                    lblFridayPM.Text = "";
                    lblSaturdayAM.Text = "";
                    lblSaturdayPM.Text = "";
                    lblSundayAM.Text = "";
                    lblSundayPM.Text = "";
                }
            }
        }

        private void btnPrintPayperiodSheet_Click(object sender, EventArgs e)
        {
            PrintingClass.PrintEmployeePayperiodTimeSheet();
        }

        private void btnPrintWeekSchedule_Click(object sender, EventArgs e)
        {
            PrintingClass.PrintEmployeeSchedulePage();
        }

        private void btnRequestTimeOff_Click(object sender, EventArgs e)
        {
            MainProgram._Main.panelControl1.Controls.Clear();
            //MainProgram._Main.panelControl1.Controls.Add(new TimeOffRequest());
        }

        private void btnClockIN_Click(object sender, EventArgs e)
        {
            try
            {

                DataRow AddPunch = DataTableClass.EmployeeTimeSheet.NewRow();

                AddPunch["EmployeeID"] = StaticProperties.ServerID.ToString();
                AddPunch["Date"] = DateTime.Today;
                AddPunch["ClockIN"] = DateTime.Now.ToString();

                DataTableClass.EmployeeTimeSheet.Rows.Add(AddPunch);
            }
            catch
            {

            }
            finally
            {
                MainProgram._Main.panelControl1.Controls.Clear();
                MainProgram._Main.panelControl1.Controls.Add(new HomeScreen());
            }
        }

        private void btnClockOUT_Click(object sender, EventArgs e)
        {
            this.listView1.Focus();
            this.listView1.Items[listView1.Items.Count - 1].Selected = true;

            //MessageBox.Show((listView1.SelectedItems[0].SubItems[4].Text));
            
            foreach (DataRow item in DataTableClass.EmployeeTimeSheet.Rows)
            {
                if(item["EntryID"].ToString().Trim() == listView1.SelectedItems[0].SubItems[4].Text)
                {
                    item["ClockOUT"] = DateTime.Now;
                }

                MainProgram._Main.panelControl1.Controls.Clear();
                MainProgram._Main.panelControl1.Controls.Add(new HomeScreen());
            }
            
        }
    }
}
